<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { statisticsSexInfo } from '@/api/charts'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    statisticsSexInfo().then(response => {
      const res = response.result
      this.initChart(res.manCount, res.womanCount, res.unlimited)
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(manData, womanData, unlimitedData) {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)',
          position:function(p){
            return [p[0] + 10, p[1] - 10]
          },
          extraCssText:'width:150px;height:54px;'
        },
        legend: {
          left: 'center',
          top: 'bottom',
          data: ['男', '女', '不限']
        },
        series: [
          {
            name: '岗位性别要求',
            type: 'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: true,
            data: [
              { value: manData, name: '男' },
              { value: womanData, name: '女' },
              { value: unlimitedData, name: '不限' }
            ]
          }
        ]
      })
    }
  }
}
</script>
